package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.EstateAdviserService;
import com.yixun.qfbao.apidto.ChangeCPAdviserReqDto;
import com.yixun.qfbao.apidto.EditEstateAdviserReqDto;
import com.yixun.qfbao.apidto.base.CommonResDTO;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.vo.ChangeCPAdviserReqVo;
import com.yixun.qfbao.vo.EditEstateAdviserReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("changeCPAdviserHander")
public class ChangeCPAdviserHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(ChangeCPAdviserHander.class);

    @Autowired
    EstateAdviserService estateAdviserService;

    /**
     * 变更置业顾问说明
     * 1、变更楼盘职业顾问,报备Id为null,必须要有旧的置业顾问参数
     * 2、变更报备置业顾问,旧置业顾问可为空.
     *  即给报备重新绑定一个其他置业顾问
     * @param reqDto
     * @return
     */
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        ChangeCPAdviserReqVo req = (ChangeCPAdviserReqVo)reqDto;
        ChangeCPAdviserReqDto reqDto1 = req.getParams();
        BaseResponseDTO<CommonResDTO> res = new BaseResponseDTO<>();
        CommonResDTO resDto = new CommonResDTO();
        res.setResult(ResultEnum.FAIL.getNo());
        try {
            if(Objects.isNull(reqDto1.getCustomerPremisesId()) && StringUtils.isBlank(reqDto1.getOldAdviser())){
                res.setErrorMsg("变更失败: 原置业顾问缺失,无法变更!");
                return res;
            }
            estateAdviserService.changeCPAdviser(reqDto1);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
        }catch (Exception e){
            logger.error("change adviser error, originanlAdviser-newAdviser {}, exception: {}", reqDto1.getOldAdviser() + "-" + reqDto1.getNewAdviser(), e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("置业顾问删除失败!");
        }
        return res;
    }
}
